package com.zoweunion.mechanic.util.task;

import com.zoweunion.mechanic.dao.app.ToolCarDataMapper;
import com.zoweunion.mechanic.model.things.ThingsData;
import com.zoweunion.mechanic.util.DateUtil;
import com.zoweunion.mechanic.util.common.CarDataStatisticsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Component
@Deprecated
public class DeviceStatisticsStoreTask {
    /** 运行时长 */
    private final String OperatingHours = "OperatingHours";

    @Autowired
    private ToolCarDataMapper toolCarDataMapper;

    // @Scheduled(cron = "0 0/1 * * * *")
    public void task() {
        List<Map<String, Object>> pcCarList = toolCarDataMapper.getAllPcCar();

        for (Map<String, Object> pcCar : pcCarList) {
            if (pcCar.get("device_template") == null) {
                continue;
            }
            String intelligentTemplateIdId = pcCar.get("device_template").toString();
            String thingsId = pcCar.get("things_id").toString();

            LocalDate endTime = LocalDate.now().minusDays(1);
            String tableName = ThingsData.getTableName(intelligentTemplateIdId);
            Map<String, Object> paramsMap = new HashMap<>(16);
            paramsMap.put("tableName", tableName);
            paramsMap.put("thingsId", thingsId);
            paramsMap.put("tag", OperatingHours);
            paramsMap.put("endTime", DateUtil.formatDate(endTime));
            paramsMap.put("startTime", DateUtil.formatDate(endTime));
            List<Map<String, Object>> carDailyWorkHoursList = toolCarDataMapper.peakDayData(paramsMap);

            List<Map<String, Object>> carHoursList = CarDataStatisticsUtil.getConsumption(carDailyWorkHoursList, OperatingHours);

            List<Map<String, Object>> carDataStatisticsList = new LinkedList<>();
            Map<String, Object> carDataStatistics;
            for (Map<String, Object> carHoursMap : carHoursList) {
                carDataStatistics = new HashMap<>(6);
                carDataStatistics.put("sample_date", carHoursMap.get("date"));
                carDataStatistics.put("OperatingHours", carHoursMap.get("value"));
                carDataStatistics.put("things_id", thingsId);
                carDataStatisticsList.add(carDataStatistics);
            }
            if (carDataStatisticsList.size() > 0) {
                toolCarDataMapper.saveCarDataStatistics(carDataStatisticsList);
            }
        }
    }
}
